package com.simen.hellobytes.android;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * author: SimenHi
 * create time: 2020-05-09 10:57
 * description:
 */
public class MobileUtil {

    public static boolean isAppAvailable(Context context, String packageName) {//本机是否安装了指定程序
        return isAvailable(context,packageName)[0];
    }

    public static boolean isSomeAvailable(Context context,String... packageName) {//本机是否安装了指定程序
        boolean[] avilibles = isAvailable(context,packageName);
        for (int index = 0; index < avilibles.length; index++) {
            if (avilibles[index]) return true;
        }
        return false;
    }

    private static boolean[] isAvailable(Context context, String... packageNames) {//本机是否安装了指定程序
        if (packageNames == null
                || packageNames.length == 0
                || TextUtils.isEmpty(packageNames[0])) {
            return new boolean[]{false};
        }

        boolean[] available = new boolean[packageNames.length];
        for (int index = 0; index < available.length; index++) {
            available[index] = false;
        }

        // 获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        // 用于存储所有已安装程序的包名
        List<String> packageNameList = new ArrayList<String>();
        // 从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNameList.add(packName);
            }
        }
        // 判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        for (int index = 0; index < packageNames.length; index++) {
            if (packageNameList.contains(packageNames[0])) {
                available[index] = true;
            }
        }
        return available;
    }

    public static int getStatusBarHeight(Context context){
        // generate a rectangle and siz is same as status bar
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");

        if(resourceId == 0){
            if(context instanceof Activity){
                Activity activity= (Activity) context;

                Window window = activity.getWindow();

                Rect rectangle = new Rect();
                window.getDecorView().getWindowVisibleDisplayFrame(rectangle);

                int statusBarTop = rectangle.top;
                int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();

                return contentViewTop - statusBarTop;
            }

            return 0;
        }

        return context.getResources().getDimensionPixelSize(resourceId);
    }

    public static Point getScreenSize(Context context) {
        WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            mWindowManager.getDefaultDisplay().getRealSize(point);
        } else {
            mWindowManager.getDefaultDisplay().getSize(point);
        }

        return point;
    }

}
